package beautiful.butterfly.drds.binlog.snapshot;

import beautiful.butterfly.drds.binlog.type.ParallelType;
import beautiful.butterfly.drds.binlog.utils.WriteResultUtils;
import com.alibaba.fastjson.JSONObject;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

public class SnapShot
{
    @Setter
    @Getter
    AtomicReference<SnapShot> nextSnapShot;


    // 全局valid标志
    @Setter
    @Getter
    private AtomicBoolean globalValid;


    // 大的类型Rotate:io
    @Setter
    @Getter
    private SnapShotType snapShotType;


    @Setter
    @Getter
    // 如果是io类型，是否接收不被过滤掉?
    private boolean accepted;


    @Setter
    @Getter
    // 如果接受，相应的数据
    private String databaseName;


    @Setter
    @Getter
    private String tableName;

    @Setter
    @Getter
    private ArrayList<JSONObject> datas;

    @Setter
    @Getter
    // 存储结果
    private AtomicInteger writeResult;// 默认失败,这个变量会在多线程情况下被使用

    // 更新到zk
    @Setter
    @Getter
    private String zkValue;

    // 并行类型：表：库
    @Setter
    @Getter
    private ParallelType parallelType;

    @Setter
    @Getter
    private ArrayList<String> pks;


    public SnapShot(AtomicBoolean _globalValid, SnapShotType _type, boolean _accepted, String _db, String _table,
                    ArrayList<JSONObject> _datas, String _zkValue, ParallelType pType, ArrayList<String> _pks)
    {
        globalValid = _globalValid;
        snapShotType = _type;
        accepted = _accepted;
        databaseName = _db;
        tableName = _table;
        datas = _datas;
        writeResult = new AtomicInteger(WriteResultUtils.PENDING.ordinal());// 默认未知情况,1表示成功，-1表示失败
        zkValue = _zkValue;
        nextSnapShot = new AtomicReference<SnapShot>(null);
        // 设置并行类型
        parallelType = pType;
        pks = _pks;
    }

    public SnapShot getNextSnapShot()
    {
        return nextSnapShot.get();
    }

    public void setNextSnapShot(SnapShot snapShot)
    {
        this.nextSnapShot.set(snapShot);
    }

    public int getWriteResult()
    {
        return writeResult.get();
    }

    public void setWriteResult(int result)
    {
        writeResult.set(result);
    }


}
